<?php


namespace Gaoop\Bundle\ApiBundle\Traits;


trait HelperTrait
{
    /**
     * 获取容器
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
     */
    protected function getContainer()
    {
        global $kernel;
        return $kernel->getContainer();
    }

    /**
     * 获取当前登录用户
     * @return string|\Stringable|\Symfony\Component\Security\Core\User\UserInterface|null
     */
    protected function getAdminUser()
    {
        $result = null;

        $container = $this->getContainer();
        if ($container->has('security.token_storage')) {
            $token = $container->get('security.token_storage')->getToken();
            if (!is_null($token)) {
                $result = $token->getUser();
            }
        }

        return $result;
    }
}